package com.sihuatech.coreagent.core.communication.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.github.pagehelper.Page;

public class PageInfo<T> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 当前页

	private int pageNum;

	// 每页的数量

	private int pageSize;

	// 总记录数

	private long total;

	// 总页数

	private int pages;

	// 结果集

	private List<T> records;

	// 是否为第一页

	private boolean isFirstPage = false;

	// 是否为最后一页

	private boolean isLastPage = false;

	public PageInfo() {

	}
	 
	public PageInfo(int pageNum ,int pageSize,long total,List<T> records) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.total = total;
		this.records = records;
	}

	public PageInfo(List<T> list) {
		if (list instanceof Page) {
			Page page = (Page) list;
			this.pageNum = page.getPageNum();
			this.pageSize = page.getPageSize();
			this.pages = page.getPages();
			this.records = page;
			this.total = page.getTotal();
		} else if (list instanceof Collection) {
			this.pageNum = 1;
			this.pageSize = list.size();
			this.pages = 1;
			this.records = list;
			this.total = list.size();
		}
		if (list instanceof Collection) {
			// 判断页面边界
			judgePageBoudary();
		}
	}

	/** 
	 ** 判定页面边界
	 **/
	private void judgePageBoudary() {
		isFirstPage = pageNum == 1;
		isLastPage = pageNum == pages;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public List<T> getRecords() {
		return records;
	}

	public void setRecords(List<T> records) {
		this.records = records;
	}

	public boolean isFirstPage() {
		return isFirstPage;
	}

	public void setFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	public boolean isLastPage() {
		return isLastPage;
	}

	public void setLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}

	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer("PageInfo{");
		sb.append("pageNum=").append(pageNum);
		sb.append(", pageSize=").append(pageSize);
		sb.append(", total=").append(total);
		sb.append(", pages=").append(pages);
		sb.append(", records=").append(records);
		sb.append(", isFirstPage=").append(isFirstPage);
		sb.append(", isLastPage=").append(isLastPage);
		sb.append(", navigatepageNums=");
		sb.append('}');
		return sb.toString();
	}

}
